package com.sunzn.media.library.adpt;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sunzn.media.library.R;
import com.sunzn.media.library.bean.Album;
import com.sunzn.media.library.bean.Item;
import com.sunzn.media.library.help.SelectedItemCollection;
import com.sunzn.media.library.view.CheckView;
import com.sunzn.media.library.view.MediaGrid;

public class MediaAdapter extends RecyclerViewCursorAdapter<RecyclerView.ViewHolder> implements MediaGrid.OnMediaGridClickListener {


    private final RecyclerView mRecyclerView;

    private int mImageResize;

    private OnMediaClickListener mOnMediaClickListener;

    private CheckStateListener mCheckStateListener;

    private final SelectedItemCollection mSelectedCollection;

    public interface CheckStateListener {

        void onUpdate();

    }

    public interface OnMediaClickListener {
        void onMediaClick(Album album, Item item, int adapterPosition);
    }

    public MediaAdapter(SelectedItemCollection collection, RecyclerView recyclerview) {
        super(null);
        mRecyclerView = recyclerview;
        mSelectedCollection = collection;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.media_grid_item, parent, false);
        return new MediaViewHolder(v);
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
        if (holder instanceof MediaViewHolder) {
            MediaViewHolder mediaViewHolder = (MediaViewHolder) holder;
            final Item item = Item.valueOf(cursor);
            Log.e("WWWWWWWWWW",item.toString());
            mediaViewHolder.mMediaGrid.preBindMedia(new MediaGrid.PreBindInfo(
                    getImageResize(mediaViewHolder.mMediaGrid.getContext()),
                    null,
                    true,
                    holder
            ));
            mediaViewHolder.mMediaGrid.bindMedia(item);
            mediaViewHolder.mMediaGrid.setOnMediaGridClickListener(this);
            setCheckStatus(item, mediaViewHolder.mMediaGrid);
        }
    }

    private void setCheckStatus(Item item, MediaGrid mediaGrid) {
        int checkedNum = mSelectedCollection.checkedNumOf(item);
        if (checkedNum > 0) {
            mediaGrid.setCheckEnabled(true);
            mediaGrid.setCheckFrame(true);
            mediaGrid.setCheckedNum(checkedNum);
        } else {
            mediaGrid.setCheckEnabled(true);
            mediaGrid.setCheckFrame(false);
            mediaGrid.setCheckedNum(checkedNum);
        }
    }

    private int getImageResize(Context context) {
        if (mImageResize == 0) {
            RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
            int spanCount = ((GridLayoutManager) lm).getSpanCount();
            int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
//            int availableWidth = screenWidth - context.getResources().getDimensionPixelSize(R.dimen.media_grid_spacing) * (spanCount - 1);
            int availableWidth = screenWidth - 9 * (spanCount - 1);
            mImageResize = availableWidth / spanCount;
//            mImageResize = (int) (mImageResize * mSelectionSpec.thumbnailScale);
            mImageResize = (int) (mImageResize * 1.0);
        }
        return mImageResize;
    }

    @Override
    protected int getItemViewType(int position, Cursor cursor) {
        return 0;
    }

    public void registerCheckStateListener(CheckStateListener listener) {
        mCheckStateListener = listener;
    }

    public void unregisterCheckStateListener() {
        mCheckStateListener = null;
    }

    public void registerOnMediaClickListener(OnMediaClickListener listener) {
        mOnMediaClickListener = listener;
    }

    public void unregisterOnMediaClickListener() {
        mOnMediaClickListener = null;
    }

    @Override
    public void onThumbnailClicked(ImageView thumbnail, Item item, RecyclerView.ViewHolder holder) {
        if (mOnMediaClickListener != null) {
            mOnMediaClickListener.onMediaClick(null, item, holder.getAdapterPosition());
        }
    }

    @Override
    public void onCheckViewClicked(CheckView checkView, Item item, RecyclerView.ViewHolder holder) {
        int checkedNum = mSelectedCollection.checkedNumOf(item);
        if (checkedNum == CheckView.UNCHECKED) {
            mSelectedCollection.add(item);
        } else {
            mSelectedCollection.remove(item);
        }
        notifyCheckStateChanged();
    }

    private void notifyCheckStateChanged() {
        notifyDataSetChanged();
        if (mCheckStateListener != null) {
            mCheckStateListener.onUpdate();
        }
    }

    private static class MediaViewHolder extends RecyclerView.ViewHolder {

        public MediaGrid mMediaGrid;

        MediaViewHolder(View itemView) {
            super(itemView);
            mMediaGrid = (MediaGrid) itemView;
        }
    }

}
